'
' Copyright (c) 2004-2005
' by Peter Donker ( info@bring2mind.net )
'
' Permission is hereby granted to use and/or modify this software for a single server. It is 
' NOT permitted to publish, distribute, sublicense, and/or sell copies of this software in its
' original or modified form. You are not allowed to remove this copyright notice.
'
' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING 
' BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
' NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 
' DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
' OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OF THE SOFTWARE.
'
Imports System
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports DotNetNuke
Imports DotNetNuke.Services.Localization.Localization
Imports Bring2mind.DNN.Modules.Guestbook.Business

Namespace Bring2mind.DNN.Modules.Guestbook.Controls

 Public Class GuestbookEdit
  Inherits ModuleBase

#Region " Controls "
  Protected WithEvents cmdUpdate As System.Web.UI.WebControls.LinkButton
  Protected WithEvents cmdCancel As System.Web.UI.WebControls.LinkButton
  Protected WithEvents cmdDelete As System.Web.UI.WebControls.LinkButton
  Protected WithEvents cmdRemovePic As System.Web.UI.WebControls.LinkButton
  Protected WithEvents txtBodyText As System.Web.UI.WebControls.TextBox
  Protected WithEvents txtBodyHtml As DotNetNuke.UI.UserControls.TextEditor
  Protected WithEvents txtEmail As System.Web.UI.WebControls.TextBox
  Protected WithEvents txtUserName As System.Web.UI.WebControls.TextBox
  Protected WithEvents chkIsApproved As System.Web.UI.WebControls.CheckBox
  Protected WithEvents FileUpload As System.Web.UI.HtmlControls.HtmlInputFile
  Protected WithEvents trPicture As System.Web.UI.HtmlControls.HtmlTableRow
  Protected WithEvents trPicture2 As System.Web.UI.HtmlControls.HtmlTableRow
  Protected WithEvents trApproved As System.Web.UI.HtmlControls.HtmlTableRow
  Protected WithEvents imgPicture As System.Web.UI.WebControls.Image
  Protected WithEvents trBodyText As System.Web.UI.HtmlControls.HtmlTableRow
  Protected WithEvents trBodyHtml As System.Web.UI.HtmlControls.HtmlTableRow

#End Region

#Region " Variables "
  Dim entryId As Integer
#End Region

#Region " Event Handlers "
  Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

   If Not (Request.Params("entryId") Is Nothing) Then

    If Me.ModuleSettings.CanApprove Or Me.IsEditable Then
     entryId = Int32.Parse(Request.Params("entryId"))
    Else ' security violation attempt to access item not related to this Module
     Response.Redirect(NavigateURL(), True)
    End If
   Else
    If Me.ModuleSettings.CanAdd Then
     entryId = -1
    Else ' security violation attempt to access item not related to this Module
     Response.Redirect(NavigateURL(), True)
    End If
   End If

   If Not Page.IsPostBack Then
    cmdDelete.Attributes.Add("onClick", "javascript:return confirm('" & GetString("DeleteItem") & "');")
    trApproved.Visible = Me.ModuleSettings.CanApprove
    If entryId <> -1 Then
     Dim objEntries As New EntriesController
     Dim objEntrie As EntriesInfo = objEntries.GetEntries(entryId, ModuleId)
     If Not objEntrie Is Nothing Then
      'set screen
      With objEntrie
       If Me.ModuleSettings.AllowHtml Then
        trBodyText.Visible = False
        txtBodyHtml.Text = .Body
       Else
        trBodyHtml.Visible = False
        txtBodyText.Text = .Body
       End If
       txtEmail.Text = .Email
       txtUserName.Text = .UserName
       chkIsApproved.Checked = .IsApproved
       cmdRemovePic.Visible = (.Picture <> "")
       trPicture2.Visible = (.Picture <> "")
       If .Picture <> "" Then
        imgPicture.ImageUrl = PicturePath & .Picture
       End If
       trPicture.Visible = False
      End With
     Else ' security violation attempt to access item not related to this Module
      Response.Redirect(NavigateURL(), True)
     End If
    Else
     'Initialize New Record
     If Me.ModuleSettings.AllowHtml Then
      trBodyText.Visible = False
     Else
      trBodyHtml.Visible = False
     End If
     If Me.UserId <> -1 Then
      txtUserName.Text = Me.UserInfo.FullName.Trim
      txtEmail.Text = Me.UserInfo.Membership.Email
     End If
     cmdDelete.Visible = False
     trPicture.Visible = Me.ModuleSettings.AllowPictures
     trPicture2.Visible = False
     cmdRemovePic.Visible = False
    End If
   Else
    'its a postback
   End If
  End Sub

  Private Sub cmdUpdate_Click(ByVal sender As Object, ByVal e As EventArgs) Handles cmdUpdate.Click
   Try
    ' Only Update if the Entered Data is Valid
    If Page.IsValid = True Then

     Dim objEntries As New EntriesController
     Dim objEntrie As EntriesInfo

     'jpp 19.10.2005
     If entryId <> -1 Then
      objEntrie = objEntries.GetEntries(entryId, ModuleId)
     Else
      objEntrie = New EntriesInfo
     End If
     'bind text values to object
     With objEntrie
      'set values
      If Me.ModuleSettings.AllowHtml Then
       .Body = txtBodyHtml.Text
      Else
       .Body = txtBodyText.Text
      End If
      .Email = txtEmail.Text
      .EntryId = entryId
      .ModuleId = ModuleId
      .UserName = txtUserName.Text
      If Me.ModuleSettings.AutoApprove Then
       .IsApproved = True
      Else
       If Me.ModuleSettings.CanApprove Then
        .IsApproved = chkIsApproved.Checked
       End If
      End If
      If Me.ModuleSettings.AllowPictures Then
       If Not IO.Directory.Exists(ThumbnailMapPath) Then
        IO.Directory.CreateDirectory(ThumbnailMapPath)
       End If
       'JpP 19.10.2005
       If Not IsNothing(FileUpload.PostedFile) Then
        If FileUpload.PostedFile.ContentLength > 0 Then
         Dim Extension As String = IO.Path.GetExtension(FileUpload.PostedFile.FileName)
         If PermittedFileExtensions.IndexOf(Extension.ToLower & ";") > -1 Then
          Dim nf As String = GetNewFilename(Extension)
          FileUpload.PostedFile.SaveAs(PictureMapPath & nf)
          Try
           Globals.ResizeImage(PictureMapPath & nf, Me.ModuleSettings.MaxPictureSize)
           Globals.ResizeImage(PictureMapPath & nf, Me.ModuleSettings.ThumbnailSize, ThumbnailMapPath)
          Catch ex As Exception
           ' Security
           IO.File.Delete(PictureMapPath & nf)
          End Try
          .Picture = nf
         End If
        End If
       End If
      End If
     End With

     'jpp 19.10.2005
     objEntries = New EntriesController
     If entryId = -1 Then
      objEntrie.DateSubmitted = Now
      entryId = objEntries.AddEntries(objEntrie)
      If Me.ModuleSettings.NotificationEmail.Trim <> "" Then
       Dim alEntry As New ArrayList
       alEntry.Add(Convert.ToString(objEntrie.UserName) & "")
       alEntry.Add(Convert.ToString(objEntrie.Email) & "")
       alEntry.Add(Convert.ToString(objEntrie.Picture) & "")
       DotNetNuke.Services.Mail.Mail.SendMail(Me.PortalSettings.Email, Me.ModuleSettings.NotificationEmail, "", GetString("Notification.Subject", Me.LocalResourceFile), GetSystemMessage(Me.PortalSettings, "Notification.Body", Me.UserInfo, Me.LocalResourceFile, alEntry) & Server.HtmlDecode(objEntrie.Body), "", "html", "", "", "", "")
      End If
      If Me.ModuleSettings.AutoApprove Or Me.ModuleSettings.CanApprove Then
       Response.Redirect(NavigateURL(), True)
      Else
       Dim d As New Bring2mind.DNN.WebControls.Dialog
       With d
        .Title = GetString("ConfirmTitle", Me.LocalResourceFile)
        .Description = GetString("ConfirmDescription", Me.LocalResourceFile)
        .AddButton(GetString("cmdOK", Me.LocalResourceFile), NavigateURL)
       End With
       Me.Controls.Clear()
       Me.Controls.Add(d)
      End If
     Else
      objEntries.UpdateEntries(objEntrie)
      Response.Redirect(NavigateURL(), True)
     End If

    End If
   Catch exc As Exception 'Module failed to load
    ProcessModuleLoadException(Me, exc)
   End Try
  End Sub

  Private Sub cmdCancel_Click(ByVal sender As Object, ByVal e As EventArgs) Handles cmdCancel.Click
   Try
    Response.Redirect(NavigateURL(), True)
   Catch exc As Exception 'Module failed to load
    ProcessModuleLoadException(Me, exc)
   End Try
  End Sub

  Private Sub cmdDelete_Click(ByVal sender As Object, ByVal e As EventArgs) Handles cmdDelete.Click
   If entryId <> -1 Then
    Dim objEntries As New EntriesController
    Dim objEntry As EntriesInfo = objEntries.GetEntries(entryId, ModuleId)
    If Not objEntry Is Nothing Then
     If Convert.ToString(objEntry.Picture) <> "" Then
      IO.File.Delete(PictureMapPath & objEntry.Picture)
      IO.File.Delete(ThumbnailMapPath & objEntry.Picture)
     End If
     objEntries.DeleteEntries(entryId)
    End If
    Me.Response.Redirect(NavigateURL, True)
   End If
  End Sub

  Private Sub cmdRemovePic_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdRemovePic.Click

   Dim objEntries As New EntriesController
   Dim objEntrie As EntriesInfo = objEntries.GetEntries(entryId, ModuleId)
   If Not objEntrie Is Nothing Then
    IO.File.Delete(ThumbnailMapPath & objEntrie.Picture)
    IO.File.Delete(PictureMapPath & objEntrie.Picture)
    objEntrie.Picture = ""
    objEntries.UpdateEntries(objEntrie)
   End If
   trPicture2.Visible = False

  End Sub
#End Region

#Region " Optional Interfaces "
#End Region

#Region " Private Methods "
#End Region

#Region " Public Methods "
#End Region

#Region " Web Form Designer Generated Code "

  'This call is required by the Web Form Designer.
  <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

  End Sub

  'NOTE: The following placeholder declaration is required by the Web Form Designer.
  'Do not delete or move it.
  Private designerPlaceholderDeclaration As System.Object

  Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
   'CODEGEN: This method call is required by the Web Form Designer
   'Do not modify it using the code editor.
   InitializeComponent()
  End Sub

#End Region

 End Class
End Namespace
